package org.xutils.http.config;


import android.database.sqlite.SQLiteException;
import android.util.Log;

import net.zetetic.database.DatabaseErrorHandler;
import net.zetetic.database.sqlcipher.SQLiteDatabase;

import org.xutils.db.DbManager;
import org.xutils.db.ex.DbException;
import org.xutils.db.xDb;

/**
 * Created by wyouflf on 15/7/31.
 * 全局db配置
 */
public enum DbConfigs {
    HTTP(new DbManager.DaoConfig()
            .setDbName("xUtils_http_cache.db")
            .setDbVersion(2)
            .setDbOpenListener(new DbManager.DbOpenListener() {
                @Override
                public void onDbOpened(DbManager db) {
                    db.getDatabase().enableWriteAheadLogging();
                }
            })
            .setDbUpgradeListener(new DbManager.DbUpgradeListener() {
                @Override
                public void onUpgrade(DbManager db, int oldVersion, int newVersion) {
                    try {
                        db.dropDb(); // 默认删除所有表
                    } catch (DbException ex) {
                        if (xDb.getInstance().isDebug()) {
                            android.util.Log.d("-H-DbConfigs", ex.toString());
                        }
                    }
                }
            })),

    COOKIE(new DbManager.DaoConfig()
            .setDbName("xUtils_http_cookie.db")
            .setDbVersion(1)
            //默认密码
            .setDbEncrypt("zzh_encrypt")
            .setDbOpenListener(db -> db.getDatabase().enableWriteAheadLogging())
            .setDbUpgradeListener((db, oldVersion, newVersion) -> {
                try {
                    db.dropDb(); // 默认删除所有表
                } catch (DbException ex) {
                    if (xDb.getInstance().isDebug()) {
                        android.util.Log.d("-H-DbConfigs", ex.toString());
                    }
                }
            }).setDatabaseErrorHandler(new DatabaseErrorHandler() {
                @Override
                public void onCorruption(SQLiteDatabase dbObj, SQLiteException exception) {
                    if (xDb.getInstance().isDebug()) {
                        Log.e("---zzh", dbObj.toString());
                    }
                }
            }));

    private DbManager.DaoConfig config;

    DbConfigs(DbManager.DaoConfig config) {
        this.config = config;
    }

    public DbManager.DaoConfig getConfig() {
        return config;
    }
}
